/*
 *  LanguageChooserDialog.java
 *
 *  Course Project for User Interface Programming I, 2011
 *  Group 19
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License
 *  as published by the Free Software Foundation; either version 2
 *  of the License, or any later version.
 *
 */
package justdoit.view;

import justdoit.view.model.LanguagesComboBoxModel;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Locale;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JPanel;
import justdoit.Startup;
import justdoit.controller.LanguageManager;

/**
 * This dialog is displayed to a user to choose a language at the first
 * execution
 *
 * The language in this Dialog will be English
 *
 * @author Can
 */
public class LanguageChooserDialog extends JDialog {

    private JPanel languagePanel;
    private JPanel actionPanel;
    private JComboBox languagesComboBox;
    private JButton okButton;
    private JButton cancelButton;
    private Locale selectedLocale;

    public LanguageChooserDialog() {
        initComponents();
        if (languagesComboBox.getItemCount() > 0) {
            languagesComboBox.setSelectedIndex(0);
        }
    }

    private void okAction() {
        setLanguage();
        this.dispose();
    }

    private void cancelAction() {
        //this.dispose();
        System.exit(0);
    }

    private void initComponents() {
        languagePanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 15));
        actionPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        languagesComboBox = new JComboBox(new LanguagesComboBoxModel(
                LanguageManager.getAvailableLocales()));
        okButton = new JButton("Ok");
        cancelButton = new JButton("Exit");

        okButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                okAction();
            }
        });

        cancelButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                cancelAction();
            }
        });

        languagePanel.add(languagesComboBox);

        actionPanel.add(okButton);
        actionPanel.add(cancelButton);

        this.setLayout(new BorderLayout());
        this.add(BorderLayout.CENTER, languagePanel);
        this.add(BorderLayout.SOUTH, actionPanel);

        this.getRootPane().setDefaultButton(okButton);

        this.setAlwaysOnTop(true);
        this.setSize(new Dimension(250, 120));
        this.setResizable(false);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        this.setModal(true);
    }

    private void setLanguage() {
        selectedLocale = ((LanguagesComboBoxModel) languagesComboBox.getModel()).getSelectedLocale();
        Startup.getDs().setLocale(selectedLocale);
    }

    @Override
    public void dispose() {
        if (selectedLocale == null) {
            System.exit(0);
        }
        super.dispose();
    }
}
